﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using BUS;

namespace TuyenDungViecLam.Admin
{
    public partial class ManageUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //lay all user
                LienKetDuLieu();
            }
        }

        private void LienKetDuLieu()
        {
            IEnumerable dsUser = User_BUS.GetAllUser();

            GrvManageUser.DataSource = dsUser;
            GrvManageUser.DataBind();
        }

        protected void GrvManageUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int userID =int.Parse( e.CommandArgument.ToString());

                bool kq = User_BUS.DeleteUSer(userID);

                //if (kq == true)
                //{
                    LienKetDuLieu();
                //}

            }

            if (e.CommandName == "Edit")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                Response.Redirect("EditUser.aspx?uid="+ userID.ToString());
            }

            if (e.CommandName == "EditRole")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                Response.Redirect("EditRoleUser.aspx?uid="+userID.ToString());

            }

        }
        protected void GrvManageUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label Status = (Label)e.Row.FindControl("Status");
                CheckBox cbchon = (CheckBox)e.Row.FindControl("cbchon");
                if ((int.Parse(Status.Text)) == 1)
                {
                    cbchon.Checked = true;
                }
                Label lbuser = (Label)e.Row.FindControl("lbuser");
                ImageButton Delete = (ImageButton)e.Row.FindControl("Delete");
                string str = "if(!confirm('Bạn có muốn xóa " + lbuser.Text + " này không?')) return false;";
                Delete.OnClientClick = str; //"if(!confirm('Bạn không muốn xóa công việc này không ?')) return false;";
            }
        }

        protected void GrvManageUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GrvManageUser.PageIndex = e.NewPageIndex;
            LienKetDuLieu();
        }

        protected void GrvManageUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void BtSearch_Click(object sender, EventArgs e)
        {
            
        
            //tim kiem user
            GrvManageUser.DataSource = User_BUS.TimKiemUser(txtUsername.Text);
            GrvManageUser.DataBind();
        
        }
    }
}
